package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrdersController {

   @Autowired
   private OrdersService ordersService;
   @Autowired
   private OrderDetailService orderDetailService;
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        R<String> r = ordersService.submit(orders);
        return r;
    }

    @GetMapping("/userPage")
    public R userPage(Integer page, Integer pageSize){
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        qw.eq(Orders::getUserId,BaseContext.getCurrentId());
        pageInfo=ordersService.page(pageInfo, qw);
        long total = pageInfo.getTotal();
        List<Orders> records = pageInfo.getRecords();
        ArrayList<OrdersDto> ordersDtos = new ArrayList<>();
        for (Orders record : records) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(record,ordersDto);
            LambdaQueryWrapper<OrderDetail> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(OrderDetail::getOrderId,record.getId());
            List<OrderDetail> list = orderDetailService.list(qw2);
            ordersDto.setOrderDetails(list);
            ordersDtos.add(ordersDto);
        }
        HashMap map = new HashMap();
        map.put("total",total);
        map.put("records",ordersDtos);
        return R.success(map);
    }

    @GetMapping("/page")
    public R page(Integer page, Integer pageSize,Long number,String beginTime,String endTime){
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        qw.eq(number!=null,Orders::getNumber,number);
        qw.gt(beginTime!=null,Orders::getOrderTime,beginTime).lt(endTime!=null,Orders::getOrderTime,endTime);
        pageInfo=ordersService.page(pageInfo,qw);
        long total = pageInfo.getTotal();
        List records = pageInfo.getRecords();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);
        return records!=null?R.success(map):R.error("查询失败！");
    }

    @PutMapping
    public R update(@RequestBody Orders orders){
        Long id = orders.getId();
        Integer status = orders.getStatus();
        LambdaUpdateWrapper<Orders> up = new LambdaUpdateWrapper<>();
        up.set(Orders::getStatus,status);
        up.eq(Orders::getId,id);
        boolean update = ordersService.update(up);
        return update?R.success("更改成功！"):R.error("更改失败！");
    }
}
